<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>类别管理</el-breadcrumb-item>
      <el-breadcrumb-item>类别修改</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <div class="header">
        <h2>账目分类列表</h2>
        <h3>【添加账目记录时 请在分类栏填入对应分类的id】</h3>
      </div>
      <div class="btn">
        <el-button type="primary" @click="AddDialogVisible = true"
          >添加分类</el-button
        >
      </div>
      <!-- 分类表格 -->
      <el-table :data="categoryList" border stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="分类ID" prop="id"></el-table-column>
        <el-table-column label="分类名字" prop="name"></el-table-column>
        <el-table-column label="分类类型" prop="type"></el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showUpdateDialog(scope.row.id)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeTransactionById(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 添加对话框 -->
      <el-dialog
        title="增加一条分类"
        :visible.sync="AddDialogVisible"
        width="30%"
      >
        <!-- 对话框主体区域 -->
        <el-form ref="AddFormRef" :model="AddForm" label-width="80px">
          <el-form-item label="分类名字">
            <el-input v-model="AddForm.name"></el-input>
          </el-form-item>
          <el-form-item label="分类类型">
            <el-select v-model="AddForm.type" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="AddDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addCategory()">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 修改对话框 -->
      <el-dialog title="修改" :visible.sync="updateDialogVisible" width="30%">
        <!-- 对话框主体区域 -->
        <el-form ref="updateFormRef" :model="updateForm" label-width="80px">
          <el-form-item label="分类名字">
            <el-input v-model="updateForm.name"></el-input>
          </el-form-item>
          <el-form-item label="分类类型">
            <el-select v-model="updateForm.type" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="AddDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="updateCategory()">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //分类表
      categoryList: [],
      //
      AddDialogVisible: false,
      //
      AddForm: {},
      //
      options: [
        {
          value: "expense",
          label: "expense",
        },
        {
          value: "income",
          label: "income",
        },
      ],
      value: "",
      updateDialogVisible: false,
      updateForm: {},
    };
  },
  created() {
    this.getCategoryList(window.sessionStorage.getItem("userId"));
  },
  methods: {
    async getCategoryList(id) {
      const { data: res } = await this.$http.get(`CategoryList/` + id);
      if (!res) return this.$message.error("fail");
      this.categoryList = res;
    },
    //增加的时候点击确定封装数据到addForm中准备提交给后端
    async addCategory() {
      this.AddForm.userId = window.sessionStorage.getItem("userId");
      //发送userid和addform到后端

      const { data: res } = await this.$http.post(`categories`, this.AddForm);
      this.AddDialogVisible = false;
      this.getCategoryList(window.sessionStorage.getItem("userId"));
    },
    async showUpdateDialog(id) {
      this.updateDialogVisible = true;
      const { data: res } = await this.$http.get(`categories/${id}`);
      this.updateForm = res;
    },

    //
    async updateCategory() {
      const { data: res } = await this.$http.put(`categories`, this.updateForm);
      if (res === 0) return this.$message.error("修改失败");
      this.$message.success("修改成功");
      this.updateDialogVisible = false;
      this.getCategoryList(window.sessionStorage.getItem("userId"));
    },
    async removeTransactionById(id) {
      //弹出确定取消框，是否删除用户
      const confirmResult = await this.$confirm(
        "请问是否要永久删除该用户",
        "删除提示",
        {
          confirmButtonText: "确认删除",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      //如果用户点击确认，则confirmResult 为'confirm'
      //如果用户点击取消, 则confirmResult获取的就是catch的错误消息'cancel'
      if (confirmResult != "confirm") {
        return this.$message.info("已经取消删除");
      }
      const { data: res } = await this.$http.delete(`categories/`+id);
      if(res===0) return this.$message.error("删除用户失败");
      this.$message.success("删除用户成功");
      this.getCategoryList(window.sessionStorage.getItem("userId"));
    },
  },
};
</script>

<style style="less" scoped>
.header {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
h3 {
  margin-top: 0px;
}
.btn {
  margin-top: 20px;
  display: flex;
  justify-content: end;
}
</style>